<template>
  <div>
    <el-dialog
      :title="formData.id?'编辑经纪':'新增经纪'"
      :visible="dialogVisible"
      width="70%"
      @close="closeFn"
    >
      <el-form ref="form" :model="formData" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" />
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="formData.desc" />
        </el-form-item>
        <el-form-item label="封面" prop="image">
          <!-- 放置上传图片 -->
          <uploadImage ref="uploadImage" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confrimFn">确 定</el-button>
        <el-button @click="closeFn">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addAgents, getAgentsDetail, editAgents } from '@/api/agents'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 表格用户数据
      formData: {
        name: '', // 经纪名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        address: '', // 地址
        desc: '', // 描述
        image: '' // 封面图
      },
      rules: {
        name: [
          { required: true, message: '经纪名称不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '经纪邮箱不能为空', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/,
            message: '请输入正确邮箱格式', trigger: 'blur'
          }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'
          }
        ],
        desc: [
          { required: true, message: '经纪描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取经纪详情
    async getAgentsDetail(id) {
      // 数据回显
      this.formData = await getAgentsDetail(id)
      // 头像回显
      this.$refs.uploadImage.fileList.push({
        url: this.formData.image
      })
    },
    // 确定按钮 新增编辑
    async confrimFn() {
      // 表单验证
      await this.$refs.form.validate()
      if (this.formData.id) {
        // 编辑
        // 当图片没有上传成功的时候不允许更新
        if (this.$refs.uploadImage.fileList.length === 0) {
          return this.$message.error('请上传图片!')
        }
        this.formData.image = this.$refs.uploadImage.fileList[this.$refs.uploadImage.fileList.length - 1].url
        await editAgents(this.formData)
        this.$message.success('更新成功')
      } else {
        // 新增
        // 当图片没有上传成功的时候不允许更新
        if (this.$refs.uploadImage.fileList[0] && !this.$refs.uploadImage.fileList[0].url) {
          return this.$message.error('图片还没有上传成功，请稍后!')
        }
        this.formData.image = this.$refs.uploadImage.fileList[0] && this.$refs.uploadImage.fileList[0].url
        await addAgents(this.formData)
        this.$message.success('添加成功')
      }
      this.closeFn()
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:dialogVisible', false)
      // 清空表单
      this.formData = {
        name: '', // 经纪名称
        email: '', // 电子邮箱，比如 example@qq.com
        phone: '', // 电话
        address: '', // 地址
        desc: '', // 描述
        image: '' // 封面图
      }
      // 清空图片
      this.$refs.uploadImage.fileList = []
      // 清除表单验证
      this.$refs.form.resetFields()
      // 刷新列表
      this.$emit('update')
    }
  }
}
</script>

<style lang="scss" >
.avatar-uploader .el-upload {
    border: 1px dashed #ddd;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

